package com.jdz.gxq.info.admin.tax.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.tax.bo.Tax;

public class TaxServiceImpl implements TaxService {

	private QueryDAO queryDAO = null;

	private UpdateDAO updateDAO = null;

	/**
	 * Tax检索分页用，查询记录条数
	 * 
	 * @return
	 */
	public int getTaxsCount(String enterpriseName) {

		int totalCount = queryDAO.executeForObject("tax.getTaxsCount",
				enterpriseName, Integer.class);

		return totalCount;
	}

	/**
	 * Tax检索
	 * 
	 * @return
	 */
	public List<Tax> getTaxs(Map<String, Object> params) {

		return queryDAO.executeForObjectList("tax.getTaxs", params);
	}

	/**
	 * 追加Tax
	 * 
	 * @param tax
	 */
	public void insertTax(Tax tax) {

		updateDAO.execute("tax.insertTax", tax);
	}

	/**
	 * 更新Tax
	 * 
	 * @param tax
	 */
	public void updateTax(Tax tax) {

		updateDAO.execute("tax.updateTax", tax);
	}

	/**
	 * 通过id得到tax
	 * 
	 * @param id
	 * @return
	 */
	public Tax getTaxById(int enterpriseId) {

		Tax tax = queryDAO.executeForObject("tax.getTaxById", enterpriseId,
				Tax.class);

		return tax;
	}

	/**
	 * 删除Tax
	 * 
	 * @param tax
	 */
	public void deleteTax(int enterpriseId) {

		updateDAO.execute("tax.deleteTaxById", enterpriseId);
	}

	/**
	 * 验证taxName是否重复 flag: 1 追加时验证 flag: 2 更新时验证
	 */
	public boolean checkTaxName(Tax paramTax, int flag) {

		Map<String, Object> params = new HashMap<String, Object>();
		// 企业名称
		params.put("enterpriseName", paramTax.getEnterpriseName());
		// 年度
		params.put("taxYear", paramTax.getTaxYear());

		Tax tax = queryDAO.executeForObject("tax.getTaxByNameAndYear", params,
				Tax.class);

		if (tax == null) {
			return true;
		}

		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {

			if (tax.getId() == paramTax.getId()) {
				return true;
			}
		}

		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}

}
